package TestPaperSystem.utils;

import sun.misc.Cleaner;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateGetter {

    private final static String datePattern = "yyyy-MM-dd HH:mm:ss";

    private static DateGetter instance;

    private DateGetter(){}
    public static synchronized DateGetter getInstance(){
        if(instance == null){
            instance = new DateGetter();
        }
        return instance;
    }

    public Date getCurrentTime(){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
        String dateString = formatter.format(currentTime);

        return formatter.parse(dateString,new ParsePosition(0));
    }

    public Date getTimeAftTime(String type , Long time ,int duration){
        Calendar cl = Calendar.getInstance();
        Date date = null;
        cl.setTimeInMillis(time);
        switch (type){
            case "MINUTE":
                cl.add(Calendar.MINUTE,duration);
                date = cl.getTime();
                break;
            case "HOUR":
                cl.add(Calendar.HOUR,duration);
                date = cl.getTime();
                break;
            case "DAY":
                cl.add(Calendar.DAY_OF_YEAR,duration);
                date = cl.getTime();
                break;
            case "MONTH":
                cl.add(Calendar.MONTH,duration);
                date = cl.getTime();
                break;
            case "YEAR":
                cl.add(Calendar.YEAR,duration);
                date = cl.getTime();
                break;
            case "SECOND":
                cl.add(Calendar.SECOND,duration);
                date = cl.getTime();
                break;
        }
        return date;

    }


    public Date getLastYearFirstDay(){
        Calendar cleaner = Calendar.getInstance();
        int year = cleaner.get(Calendar.YEAR);
        cleaner.clear();
        cleaner.set(Calendar.YEAR,year);
        return cleaner.getTime();
    }

    public Date getLastYearLastDay(){
        Calendar cleaner = Calendar.getInstance();
        int year = cleaner.get(Calendar.YEAR);
        cleaner.clear();
        cleaner.set(Calendar.YEAR,year);
        cleaner.roll(Calendar.DAY_OF_YEAR, -1);
        return cleaner.getTime();
    }

}
